﻿using System;
using GDIT.BLL;
using GDIT.Model;
using GDIT.Base.PageBaseHelper;

namespace GDIT.page.manage.user
{
    public partial class editPwd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(Session["UserName"].ToString()))
                {
                    PageBase.MessageBoxShowAndGo("检查到你还没有登录，请先登录，谢谢！", "~/page/manage/login.aspx", Page);
                    return;
                }
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("操作失败，原因：" + ex.Message.ToString(), Page);
            }
        }
        
        private BUsers user = null;

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                bool result = CheckUserPassword();
                if (result == false)
                {
                    PageBase.MessageBoxShow("抱歉，旧密码错误，请重新核对信息，再次操作！", Page);
                    return;
                }
                user = new BUsers();
                bool updateResult = user.UpdatePassword(Session["UserName"].ToString(), this.txtNew.Text.Trim());
                if (updateResult == false)
                {
                    PageBase.MessageBoxShow("抱歉，密码修改失败！", Page);
                    return;
                }
                PageBase.MessageBoxShow("密码修改成功！", Page);
            }
            catch (Exception ex)
            {
                PageBase.MessageBoxShow("抱歉，操作失败，请重新操作！",Page);
            }
        }

        /// <summary>
        /// 检查用户的旧密码是否正确
        /// </summary>
        /// <returns></returns>
        private bool CheckUserPassword()
        {
            user = new BUsers();
            Mtb_Users userInfo = user.GetUser(Session["UserName"].ToString());
            if (this.txtOld.Text.Trim() == userInfo.UserPwd)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
    }
}